package org.unicorn.android.base.net.client

import android.util.Log.VERBOSE
import org.unicorn.android.base.AppConfig
import org.unicorn.android.base.net.intercept.IecasTokenIntercept
import com.ihsanbal.logging.Level
import com.ihsanbal.logging.LoggingInterceptor
import org.unicorn.android.base.net.intercept.IecasLoginIntercept
import okhttp3.OkHttpClient
import java.net.Proxy
import java.util.concurrent.TimeUnit

class IecasOkhttpClient {

    companion object {

        private const val CONNECT_TIME = 15000L
        val client by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            return@lazy OkHttpClient.Builder()
                .connectTimeout(CONNECT_TIME, TimeUnit.MILLISECONDS)
                .readTimeout(CONNECT_TIME, TimeUnit.MILLISECONDS)
                .writeTimeout(CONNECT_TIME, TimeUnit.MILLISECONDS)
                .sslSocketFactory(
                    SSLSocketClient.getSSLSocketFactory(),
                    SSLSocketClient.TrustAllManager()
                )
                .hostnameVerifier(SSLSocketClient.getHostnameVerifier())
                .addInterceptor(IecasTokenIntercept())
                .addInterceptor(IecasLoginIntercept())
                .addInterceptor(
                    LoggingInterceptor.Builder()
                        .setLevel(if (AppConfig.isDebug) Level.BASIC else Level.NONE)
                        .log(VERBOSE)
                        .build()
                )
                .proxy(Proxy.NO_PROXY)
                .build()
        }


    }
}